local skel = fk.CreateSkill {
  name = "emo__jingling",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["emo__jingling"] = "净灵",
  [":emo__jingling"] = "锁定技，回合开始时，你移除所有“净”“灵”标记并将任意张手牌标记为“净”，其余标记为“灵”，这些牌不计入你手牌上限；一名角色受到伤害后，你可对其使用一张非你手牌区的“净”或“灵”并移除此牌标记（无视合法性）。",

  ["@@EmoJingMark"] = "净",
  ["@@EmoLingMark"] = "灵",
  ["#emo__jingling-mark"] = "净灵：将任意张手牌标记为“净”，其余手牌标记为“灵”",
  ["#emo__jingling-use"] = "净灵：你可对 %src 使用一张非你手牌区的“净”或“灵”",
  ["$EmoChangeRhyme"] = "%from 的 %arg 转韵为【%arg2】",
}

--- 转韵
---@param player ServerPlayer
local function changeRhyme(player)
  local skillName = "emo__diyun"
  player:setSkillUseHistory(skillName, 0, Player.HistoryTurn)
  if not player:hasSkill(skillName, true) then return end
  local mark = player:getSwitchSkillState(skillName, true)
  player.room:setPlayerMark(player, MarkEnum.SwithSkillPreName .. skillName, mark)
  player.room:sendLog{ type = "$EmoChangeRhyme", from = player.id, arg = skillName, arg2 = (mark == fk.SwitchYang and "平" or "仄") }
end


skel:addEffect(fk.TurnStart, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local tag = player.tag[skel.name]
    if tag ~= nil then
      for _, id in ipairs(player.tag[skel.name]) do
        local card = Fk:getCardById(id)
        room:setCardMark(card, "@@EmoJingMark", 0)
        room:setCardMark(card, "@@EmoLingMark", 0)
      end
    end
    local hand = player:getCardIds("h")
    player.tag[skel.name] = hand
    if #hand > 0 then
      local cards = room:askToCards(player, {
        include_equip = false, cancelable = false, min_num = 0, max_num = 9999, skill_name = skel.name,
        prompt = "#emo__jingling-mark"
      })
      for _, id in ipairs(hand) do
        local card = Fk:getCardById(id)
        if table.contains(cards, id) then
          room:setCardMark(card, "@@EmoJingMark", 1)
        else
          room:setCardMark(card, "@@EmoLingMark", 1)
        end
      end
    end
  end,
})

skel:addEffect(fk.Damaged, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(skel.name) or target.dead then return false end
    local hand = player:getCardIds("h")
    local tag = player.tag[skel.name]
    if tag == nil or #tag == 0 then return false end
    return table.find(tag, function(id) return not table.contains(hand, id) end)
  end,
  on_cost = function (self, event, target, player, data)
    local hand = player:getCardIds("h")
    local tag = player.tag[skel.name]
    if tag == nil or #tag == 0 then return false end
    local cards = table.filter(tag, function(id) return not table.contains(hand, id) end)
    local chosen = player.room:askToChooseCardsAndChoice(player, {
      cards = cards, min_num = 1, max_num = 1, choices = {"OK"}, cancel_choices = {"Cancel"}, skill_name = skel.name,
      prompt = "#emo__jingling-use:" .. target.id
    })
    if #chosen > 0 then
      event:setCostData(self, { cards = chosen })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cid = event:getCostData(self).cards[1]
    local card = Fk:getCardById(cid)
    local tag = player.tag[skel.name]
    table.removeOne(tag, cid)
    player.tag[skel.name] = tag
    room:setCardMark(card, "@@EmoJingMark", 0)
    room:setCardMark(card, "@@EmoLingMark", 0)
    -- 我将对角色使用闪和无懈，如何呢？
    room:useCard{
      from = player, tos = {target} , card = card, extraUse = true,
    }
    changeRhyme(player)
  end,
})

skel:addEffect("maxcards", {
  exclude_from = function (self, player, card)
    if player:hasSkill(skel.name) then
      return card:getMark("@@EmoJingMark") ~= 0 or card:getMark("@@EmoLingMark") ~= 0
    end
  end,
})


return skel
